# -*- coding:utf-8 -*-
# Created by Jin(jinzhencheng@outlook.com) at 2018/9/18 11:07

from scrapy import Spider
from scrapy import Request
from bd_spider.config import SpiderConfig
from bd_spider.items import HotNews


class HotSpider(Spider):

    name = SpiderConfig.HOT_SPIDER_NAME

    def start_requests(self):
        yield Request(url=SpiderConfig.HOT_HOME_URL)

    def parse(self, response):
        for li_sel in response.xpath("//div[@class='hblock']/ul/li[position()>1]"):
            type = li_sel.xpath("./a/@title").extract()[0]
            href = li_sel.xpath("./a/@href").extract()[0].strip()
            next_url = SpiderConfig.HOT_HOME + href[1:]
            yield Request(url=next_url, meta={"type": type, "type_src": next_url}, callback=self.parse_content)

    def parse_content(self, response):
        for tr_sel in response.xpath("//table/tr[position()>1]"):
            if tr_sel.xpath("./td[@class='keyword']"):
                title = tr_sel.xpath("./td[@class='keyword']/a/text()").extract()[0]
                title_src = tr_sel.xpath("./td[@class='keyword']/a/@href").extract()[0]
                icon_class = tr_sel.xpath("./td[@class='last']/span/@class").extract()[0]
                search_count = tr_sel.xpath("./td[@class='last']/span/text()").extract()[0]
                item = HotNews()
                item["type"] = response.meta["type"]
                item["type_src"] = response.meta["type_src"]
                item["trend"] = "up" if "icon-rise" == icon_class else "down"
                item["search_count"] = int(search_count)
                item["title"] = title
                item["title_src"] = title_src
                yield item

